<?php
/**
 * Created by PhpStorm.
 * User: DaibiDaddy
 * Date: 2016/5/24
 * Time: 17:14
 */

namespace App\Base;

use App\Model\LazyCommon\AdminLogModel;
use App\Model\LazyCommon\CCM;
use Symfony\Component\HttpFoundation\Request;

class BaseController4Admin extends BaseController {

    public $data;

    public function needToBeLogin()
    {
        $url = $this->generateUrl("AdminLogin");
        header("location:$url");
        die();
    }

    public function isAdmin()
    {
        $roles = $this->getSessionData("role");
        if (is_array($roles) && in_array("ROLE_ADMIN",$roles)) {
            return $this->getSessionData("id");
        } else {
            return false;
        }
    }

    public function needToBeAdmin()
    {
        if ($this->isAdmin()) {
            return $this->getSessionData("id");
        } else {
            $this->needToBeLogin();
        }
    }

    public function addLog($aid,$txt)
    {
        $request = Request::createFromGlobals();
        $AdminLogModel = new AdminLogModel($this->getDoctrine());
        $AdminLogModel->addLog($aid,$txt,$request);
    }

    //根据用户分组和角色匹配角色的权限
    public function isCompanyAdmin()
    {
        $role = $this->getSessionData("groups");
        if ($role == 4) {
            return $this->getSessionData("id");
        } else {
            return false;
        }
    }
    public function isAdminRole($role)
    {
        $roles = $this->getSessionData("role");
        if (is_array($roles) && in_array($role,$roles)) {
            return $this->getSessionData("id");
        } else {
            return false;
        }
    }

    public function getUserSiteStatus()
    {
        $CCM = new CCM($this->getDoctrine());
        $siteStatus = [];
        if (!$this->isAdminManager()) {
            //不是最高管理员权限
            $roles = ["ROLE_ADMIN_WY","ROLE_ADMIN_JL","ROLE_ADMIN_FLDZ","ROLE_ADMIN_RJDZ","ROLE_ADMIN_ZYGL"];
            foreach ($roles as $role) {
                if ($this->isAdminRole($role)) {
                    $siteStatus[] = $CCM->getOneByLink($role)->getId();
                }
            }
        }
        return $siteStatus;
    }
}
